package com.ctsi.framework.common.response;

import com.ctsi.framework.common.constant.ResponseCodeEnum;

import java.io.Serializable;

/**
 * 响应模型，用于统一请求响应处理<br/>
 *
 * @param <E> 实际对象类型
 *
 * @author zhangjw
 */
@Deprecated
public class UnifyResponse<E> implements Serializable {

    private boolean success;
    /**
     * 响应编码
     */
    private String code;
    /**
     * 错误描述
     */
    private String message;
    /**
     * 返回的实际对象
     */
    private E body;

    public UnifyResponse() {
    }

    private UnifyResponse(boolean success , ResponseCodeEnum code, E body) {
        this.success = success;
        this.code = code.code();
        this.message = code.msg();
        this.body = body;
    }

    private UnifyResponse(String code,String message){
        this.success = false;
        this.code = code;
        this.message = message;
        this.body = null;
    }

    public String getCode() {
        return code;
    }


    public String getMessage() {
        return message;
    }


    public E getBody() {
        return body;
    }

    /**
     * 返回是否成功
     */
    public boolean ok() {
        return this.success;
    }

    /**
     * 返回成功
     *
     * @param body 实际对象
     */
    public static <E> UnifyResponse<E> success(E body) {
        return new UnifyResponse<>(true, ResponseCodeEnum.GL99990200, body);
    }

    /**
     * 返回错误信息
     * @param code
     * @param <E>
     * @return
     */
    public static <E> UnifyResponse<E> error(ResponseCodeEnum code){
        return new UnifyResponse<>(false,code,null);
    }

    public static <E> UnifyResponse<E> error(String code,String message){
        return new UnifyResponse<>(code,message);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setBody(E body) {
        this.body = body;
    }
}
